home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.orig.lzh / batch / batchsplit < prev    next >
Text File  |  1989-06-27  |  2KB  |  91 lines

  1. #! /bin/sh
  2. # Prepare some batches of size $1 in files named togo.[0-9] .  We prepare a
  3. # total of 7 to stay within awk's limits on file descriptors (we need a
  4. # couple of other descriptors).  Source is file togo.next if it exists, or
  5. # togo if not.  If there is no togo.next and there is more in togo than will
  6. # fit in the numbered batches, put the next few lots in togo.next.  This
  7. # avoids the need to paw through the whole togo file every time when a large
  8. # backlog has built up.
  9. #
  10. # Buglet:  does not count the "#! rnews nnnnn" headers in sizes.
  11. #
  12. # If the togo file does not contain file sizes, we make an arbitrary guess
  13. # at an average size.
  14.  
  15. case $#
  16. in
  17.     0)
  18.     echo 'Usage: batchsplit size' >&2
  19.     exit 2
  20.     ;;
  21. esac
  22.  
  23. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  24. . ${NEWSCONFIG-/usr/lib/news/bin/config}
  25.  
  26. PATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  27. umask $NEWSUMASK
  28.  
  29. # Locking.
  30. lock="$NEWSCTL/LOCK"
  31. ltemp="$NEWSCTL/L.$$"
  32. echo $$ >$ltemp
  33. trap "rm -f $ltemp ; exit 0" 0 1 2 15
  34. while true
  35. do
  36.     if newslock $ltemp $lock
  37.     then
  38.         trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
  39.         break
  40.     fi
  41.     sleep 30
  42. done
  43.  
  44. rm -f togo.overflow togo.count
  45. if test -s togo.next
  46. then
  47.     input=togo.next
  48. else
  49.     input=togo
  50. fi
  51.  
  52. awk 'BEGIN { total = 0 ; ninbatch = 0 ; bno = 1 ; limit = '$1'
  53.         batch = "togo." bno ; nbatches = 7 }
  54.     {
  55.         if (NF == 1)
  56.             size = 3000    # Arbitrary guess.
  57.         else
  58.             size = $2
  59.         if (total + size > limit && ninbatch > 0) {
  60.             # Go to next batch.
  61.             bno++
  62.             if (bno <= nbatches) {
  63.                 batch = "togo." bno
  64.                 ninbatch = 0
  65.             } else if (bno == nbatches+1 && FILENAME == "togo") {
  66.                 batch = "togo.next"
  67.                 limit = 4 * nbatches * limit
  68.             } else {
  69.                 print NR - 1 >"togo.count"
  70.                 exit
  71.             }
  72.             total = 0
  73.         }
  74.         ninbatch++
  75.         total += size
  76.         print >batch
  77.     }' $input
  78.  
  79. if test -s togo.count
  80. then
  81.     sed "1,`cat togo.count`d" $input >togo.overflow
  82.     rm togo.count
  83. fi
  84.  
  85. if test -r togo.overflow
  86. then
  87.     mv togo.overflow $input
  88. else
  89.     >$input
  90. fi
  91.